package com.company.tips.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.company.corelib.facade.ArticleFacade;
import com.company.corelib.facade.CategoryFacade;
import com.company.corelib.model.Article;
import com.company.corelib.model.Category;

@Controller
@RequestMapping("admin/article/")
public class ArticleController {
	private ArticleFacade facade = new ArticleFacade();
	private CategoryFacade catFacade = new CategoryFacade();
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam(value="catId", required=false) String catId) {
		ModelAndView model = new ModelAndView();
		List<Article> articles = new ArrayList<Article>();
		if(catId == null || catId.equals("")){
			articles = facade.getAllArticle();
		}else{
			articles = facade.getAllArticleByCategoryId(catId);
		}
		model.addObject("articles", articles);
		model.setViewName("admin/article/list");
		return model;
	}
	
	@RequestMapping(value="create", method = RequestMethod.POST)
	public ModelAndView create(
			//@RequestParam(value="userId") int userId,
			@RequestParam(value="title", required=false) String title,
			@RequestParam(value="category", required=false) String categoryId,
			@RequestParam(value="summary", required=false) String summary,
			@RequestParam(value="content", required=false) String content,
			@RequestParam(value="tags", required=false) String tags,
			@RequestParam(value="image", required=false) String image,
			@RequestParam(value="status", required=false) String status
			){
		ModelAndView model = new ModelAndView();
		List<Category> categories = new ArrayList<Category>();
		categories = catFacade.getAllCategory();
		System.err.println("1" + categories.size());
		model.addObject("categories", categories);
		if(title == null || title.equals("")){
			model.setViewName("admin/article/create");
		}else{
			Article article = new Article();
			article.setUserId(1);
			article.setTitle(title);
			article.setCategoryId(Integer.parseInt(categoryId));
			article.setSummary(summary);
			article.setContent(content);
			article.setTag(tags);
			article.setImage(image);
			article.setActive(Integer.parseInt(status));
			article.setLikecount(0);
			facade.create(article);
			model.setViewName("redirect:list");
		}
		return model;
	}
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	public ModelAndView update(
			@RequestParam(value="articleid", required=false) String id,
			@RequestParam(value="title", required=false) String title,
			@RequestParam(value="category", required=false) String categoryId,
			@RequestParam(value="summary", required=false) String summary,
			@RequestParam(value="content", required=false) String content,
			@RequestParam(value="tags", required=false) String tags,
			@RequestParam(value="image", required=false) String image,
			@RequestParam(value="status", required=false) String status
			){
		ModelAndView model = new ModelAndView();
		List<Category> categories = new ArrayList<Category>();
		categories = catFacade.getAllCategory();
		model.addObject("categories", categories);
		Article article = new Article();
		article = facade.getArticleById(id);
		if(title == null || title.equals("")){
			model.addObject("article",article);
			model.setViewName("admin/article/edit");
		}else{
			article.setUserId(1);
			article.setTitle(title);
			article.setCategoryId(Integer.parseInt(categoryId));
			article.setSummary(summary);
			article.setContent(content);
			article.setTag(tags);
			article.setImage(image);
			article.setActive(Integer.parseInt(status));
			article.setLikecount(0);
			facade.update(article);
			model.setViewName("redirect:list");
		}
		return model;
	}
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public ModelAndView delete(
			@RequestParam(value="articleid", required=false) String articleid){
		System.err.println("articleid = " + articleid);
		ModelAndView model = new ModelAndView();
		facade.delete(articleid);
		model.setViewName("redirect:list");
		return model;
	}
}
